package Fly.example.controller;

import Fly.example.common.R;
import Fly.example.pojo.Employee;
import Fly.example.service.EmployeeServeice;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeServeice employeeServeice;

    /*
    * 员工登录业务
    * */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        log.info("登录业务开始…………");
        //将页面提交的没密码进行md5加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //用户名查询
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<Employee>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeServeice.getOne(queryWrapper);

        if (emp == null) {
            return R.error("登陆失败，没有该用户名");
        }
        //密码比对
        if (!emp.getPassword().equals(password)) {
            return R.error("登陆失败，密码错误");
        }

        //账号状态查询
        if (emp.getStatus() == 0){
            return R.error("<登录失败，账号已禁用！！");
        }

        //如果运行到这里，说明账号密码都正确，并且账号的状态正常
        request.getSession().setAttribute("employee", emp.getId());
        return R.success(emp);
    }

    /*
    * 员工退出业务
    * */
    @PostMapping("/logout")
    public R<String> exit(HttpServletRequest request){
        log.info("员工退出业务开始…………");
        request.getSession().removeAttribute("employee");
        return R.success("退出登录成功");
    }

    /*员工查询业务*/
    @GetMapping("/page")
    public R<Page<Employee>> page(HttpServletRequest request,Integer page, Integer pageSize, String name) {
        log.info("员工查询业务开始…………");
        Page<Employee> pageInfo = new Page<Employee>(page, pageSize);//分页构造器
        //条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.like(name!=null,Employee::getName,name);
        //添加排序条件
        queryWrapper.orderByDesc(Employee::getCreateTime);
        //执行查询的id
        long seId = (long) request.getSession().getAttribute("employee");
        //添加查询的条件，排除登录人的id
        queryWrapper.ne(Employee::getId,1);
        queryWrapper.ne(seId != 1,Employee::getId,seId);
        //执行查询
        employeeServeice.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }

    /*新增员工业务*/
    @PostMapping
    public R<String> insertEmployee(HttpServletRequest request,@RequestBody Employee employee) {
        log.info("新增员工的信息{}", employee.toString());

        //设置其他的信息
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        /*employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        //获取当前登录用户的ID
        long creadId = (long) request.getSession().getAttribute("employee");
        employee.setCreateUser(creadId);
        employee.setUpdateUser(creadId);*/
        //调用mybatis-puls已经封装好的的save方法来新增数据
        employeeServeice.save(employee);
        return R.success("新增员工成功");
    }

    /*根据id查询员工*/
    @GetMapping("/{id}")//有问题，前端返回来的数据后面两位数会变成0，要想解决这个问题估计
    //要去改一下传给前端的数据，因为前端的javascript的number类型有个最大值，
    //最长为16位数，而我们传给前端的数据为18位
    //使用我们自己的消息转换器就可以了，将发送给前端的消息序列化，这样就不会出现精度缺失的问题了
    public R<Employee> getByid(@PathVariable long id){
        log.info("根据id查询员工");
        Employee emp = employeeServeice.getById(id);
        return R.success(emp);
    }
    
    /*禁用员工，把status改为0
    * 启用员工，将status改为1,包括编辑员工，即更新员工
    * */
    @PutMapping
    public R<String> forbidden(HttpServletRequest request,@RequestBody Employee emp) {
        emp.setUpdateTime(LocalDateTime.now());
        //获取当前登录的id
        //获取当前登录用户的ID
        /*long creadId = (long) request.getSession().getAttribute("employee");
        emp.setUpdateUser(creadId);*/
        employeeServeice.updateById(emp);
        return R.success("员工状态修改成功");
    }
}
